import { GameComponent, GameState } from "@sapi-game/main";
import { PillarOfFortuneContext, PillarOFFortunePlayer } from "../module";
import { Dimensions } from "@sapi-game/constants";
import { Vector3Utils } from "@sapi-game/utils";
import { POFStatic } from "../static";
import { BlockVolume, Vector3 } from "@minecraft/server";

export class PillarOfFortuneWatingRoom extends GameComponent<
    GameState<PillarOFFortunePlayer, PillarOfFortuneContext>
> {
    offset: Vector3 = { x: -1, y: POFStatic.watingRoomOffsetY, z: -1 };
    override onAttach(): void {}

    destroyWatingRoom() {
        const startPoints = this.context.map.startPoints;
        for (let i = 0; i < startPoints.length; i++) {
            const size = POFStatic.waitingRoomStructure.get()?.size;
            if (!size) throw new Error("无法获取结构");
            const pos1 = Vector3Utils.add(startPoints[i], this.offset);
            const pos2 = Vector3Utils.add(pos1, {
                x: size.x,
                y: size.y,
                z: size.z,
            });
            Dimensions.Overworld.fillBlocks(
                new BlockVolume(pos1, pos2),
                "minecraft:air"
            );
        }
    }

    placeWatingRoom() {
        const startPoints = this.context.map.startPoints;
        const playerCnt = this.context.players.validSize;
        for (let i = 0; i < startPoints.length && i < playerCnt; i++) {
            const pos = Vector3Utils.add(startPoints[i], this.offset);
            POFStatic.waitingRoomStructure.placeOn(pos, Dimensions.Overworld);
        }
    }
}
